<?php
ArrowController::importModel("beans.ArrowBeanDescriptor");
ArrowController::importModel("beans.ArrowBeansException");
ArrowController::importModel("beans.interfaces.ArrowActionBean");
ArrowController::importModel("beans.interfaces.ArrowViewHelperBean");

class ArrowBeansStructure implements IArrowCacheable, IArrowSingleton{

	/**
	* Configuration from cache provider
	* 
	* @var Array
	*/
	private $configuration;

	/**
	 * Templates Structure instance
	 * 
	 * @var TemplateList[]
	 */
	private static  $selfInstances = null;


	/**
	 * Singleton !NO_REMOTE
	 * 
	 * @param ArrowProject $projectReference
	 * @return ArrowBeansStructure Default instance
	 */
	public static function getDefault(  ) {
		if ( self::$selfInstances == null ) {
			self::$selfInstances = new ArrowBeansStructure( );
		}
		return self::$selfInstances ;
	}
	

	
		
	/**
	 * Constructor !NO_REMOTE
	 * 
	 * @param ArrowProject $projectReference
	 */
	
	private function __construct( ) {
		$confFile = ArrowProject::PATH . "/" . ArrowProject :: BEANS_CONF;
		$cacheFile = ArrowCacheProvider::getFileCache( $this, $confFile, array( "file_prefix" => "beans_structure" )  );
		require_once $cacheFile;
		$this->configuration = $cache;
		$this->sourcesDir = ArrowProject::PATH."/beans";
	}
	
	/**
	 * IArrowCacheable implementation
	 *
	 * @param Array $params
	 * @return Array
	 */
	public function generateCache($params){
		$tmp = array();
		$conf = ArrowController::getDefault()->getProject()->getXMLConfig(ArrowProject::BEANS_CONF);
		$tmp["beans"] = array();
		foreach( $conf->beans->bean as $bean ){
			$tmpBean = array();
			$tmpBean["name"] = (string) $bean["name"];
			$tmpBean["path"] = (string) $bean["path"];
			$tmpBean["type"] = (string) $bean["type"];
			$tmpBean["description"] = (string) $bean["description"];
			$tmp["beans"][ (string) $bean["id"] ] = $tmpBean;
		}
		return $tmp;
	}
	
	/**
	 * Returns all templates
	 *
	 * @return ArrowBeanDescriptor[]
	 */
	public function getBeans(){
		$beans = array();
		foreach( $this->configuration["beans"] as $beanId => $bean ){
			$beans[] = $this->getBeanById($beanId);
		}
		return $beans;
	}
	
	/**
	 * Returns template by id
	 * 
	 * @param integer $templateId Action id
	 * @return ArrowBeanDescriptor
	 */
	public function getBeanById( $beanId ){
		if (!isset ($this->configuration["beans"][$beanId]))
			throw new ArrowBeansException(array (
					"msg" => "Bean doesn't exist",
					"id" => $beanId
					));
		$registred = $this->configuration["beans"][$beanId];
		$template = new ArrowBeanDescriptor($this, $beanId, $registred["name"],  $this->sourcesDir.$registred["path"],  $registred["type"], $registred["description"]);
		return $template;
	}
	

	
	/**
	 * Returns bean by mapping path
	 *
	 * @param string $path
	 * @return ArrowBeanDescriptor
	 */
	public function getBeanByMappingPath($path){
		$tmp = explode( "::", $path );
		$condiions = array( "path" => $tmp[0], "name" => $tmp[1] );
		return $this->findBean( $condiions );
	}
	
	/**
	 * Returns bean ID by mapping path
	 *
	 * @param unknown_type $path
	 * @return unknown
	 */
	public function getBeanIdByMappingPath($path){
		$tmp = explode( "::", $path );
		$args = array( "path" => $tmp[0], "name" => $tmp[1] );
		foreach( $this->configuration["beans"] as $id => $bean ){
			if(
				$args["name"] == $bean["name"] &&
				$args["path"] == $bean["path"]
			)
				return $id;
		}
		
		throw new ArrowBeansException(array (
				"msg" => "Bean doesn't exist",
				"args" => print_r( $args, 1 )
				));
	}	

	
	/**
	 * finding bean by parameters
	 *
	 * @param String[] $args
	 * @return ArrowBeanDescriptor
	 */
	public function findBean( $args ){
		$result = 0;
		foreach( $this->configuration["beans"] as $id => $bean ){
			if(
				$args["name"] == $bean["name"] &&
				$args["path"] == $bean["path"]
			){
				$result = $id;
				break;
			}
		}
		if (empty ($result))
			throw new ArrowBeansException(array (
					"msg" => "Bean doesn't exist",
					"args" => print_r( $args, 1 )
					));
		return $this->getBeanById($id);
	}

	/**
	 * Makes file structure for template
	 *
	 * @param ArrowBeanDescriptor $bean
	 */
	public function makeFileStructure( $bean ){
		$blocks = $bean->getSchema()->getBlocksNames();

		if(!file_exists($bean->getPath()))
		mkdir( $bean->getPath(), 0777, true );

		foreach($blocks as $block){
			file_put_contents( $bean->getPath()."/".$block.".tpl", "Block: ".$block );
		}


		
	}
	

}
?>
